const db = require('../db/index.js')
exports.getOrdersList = (req, res) => {
    let sql1 = 'select * from wm_order'
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(sql1, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = '数据库查询错误';
            return res.send(obj);
        }
        obj.meta.status = 200;
        obj.meta.msg = '订单列表获取成功';
        obj.data = results;
        return res.send(obj);
    })

}

exports.searchOrderById = (req, res) => {
    let id = req.params.id;
    let sql1 = 'select * from wm_order where order_id = ?'
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(sql1, id, (err, results) => {
            if (err) {
                console.log(err);
                obj.meta.status = 500;
                obj.meta.msg = '数据库查询错误';
                return res.send(obj);
            } else {
                if (results.length === 1) {
                    var data1 = {
                        "order_id": results[0].order_id,
                        "order_num": results[0].order_num,
                        "order_dissatisfied": results[0].order_dissatisfied,
                        "order_pass": results[0].order_pass,
                        "order_success": results[0].order_success,
                        "ps_num": results[0].ps_num,
                    }
                    obj.meta.msg = '查询成功';
                    obj.meta.status = 200;
                    obj.data = data1;
                    return res.send(obj)
                }
            }
        })
        //根据id查询sp_manager,满足条件返回给前端

}


exports.editOrderById = (req, res) => {
    let id = req.params.id;
    let { ordernum, orderdissatisfied, orderpass, ordersuccess, psnum } = req.body;
    let changeValue = {
        order_num: ordernum,
        order_dissatisfied: orderdissatisfied,
        order_pass: orderpass,
        order_success: ordersuccess,
        ps_num: psnum,

    }
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    let sql1 = 'update wm_order set ? where order_id=?';
    db.query(sql1, [changeValue, id], (err, results) => {
        if (err) {
            console.log(err);
            obj.meta.status = 500;
            obj.meta.msg = '数据库查询错误';
            return res.send(obj);
        } else {
            if (results.affectedRows == 1) {
                obj.meta.status = 200;
                obj.meta.msg = '修改订单信息成功';
                return res.send(obj);
            }
        }
    })

}